package com.ruoyi.system.service.impl;

import java.util.List;
import java.util.Random;

import com.ruoyi.common.utils.Assert;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.ATrainInfor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.ATrainMapper;
import com.ruoyi.system.domain.ATrain;
import com.ruoyi.system.service.IATrainService;

/**
 * 培训类别Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-05-29
 */
@Service
public class ATrainServiceImpl implements IATrainService 
{
    @Autowired
    private ATrainMapper aTrainMapper;

    /**
     * 查询培训类别
     * 
     * @param id 培训类别主键
     * @return 培训类别
     */
    @Override
    public ATrain selectATrainById(Long id)
    {
        return aTrainMapper.selectATrainById(id);
    }

    /**
     * 查询培训类别列表
     * 
     * @param aTrain 培训类别
     * @return 培训类别
     */
    @Override
    public List<ATrain> selectATrainList(ATrain aTrain)
    {
        return aTrainMapper.selectATrainList(aTrain);
    }

    /**
     * 新增培训类别
     * 
     * @param aTrain 培训类别
     * @return 结果
     */
    @Override
    public int insertATrain(ATrain aTrain)
    {
        aTrain.setCreateTime(DateUtils.getNowDate());
        return aTrainMapper.insertATrain(aTrain);
    }

    /**
     * 修改培训类别
     * 
     * @param aTrain 培训类别
     * @return 结果
     */
    @Override
    public int updateATrain(ATrain aTrain)
    {
        aTrain.setUpdateTime(DateUtils.getNowDate());
        return aTrainMapper.updateATrain(aTrain);
    }

    /**
     * 批量删除培训类别
     * 
     * @param ids 需要删除的培训类别主键
     * @return 结果
     */
    @Override
    public int deleteATrainByIds(Long[] ids)
    {
        return aTrainMapper.deleteATrainByIds(ids);
    }

    /**
     * 删除培训类别信息
     * 
     * @param id 培训类别主键
     * @return 结果
     */
    @Override
    public int deleteATrainById(Long id)
    {
        return aTrainMapper.deleteATrainById(id);
    }

    /**
     * 随机生成6位数字
     * @return
     */
    @Override
    public String randomPass() {
        return String.valueOf( new Random().nextInt(900000) + 100000);
    }

    @Override
    public List<ATrain> selectATrainXcxList(ATrain aTrain) {
        List<ATrain> list = aTrainMapper.selectATrainXcxList(aTrain);
        for (ATrain at:list) {
            if(StringUtils.isNotEmpty(at.getTrainWord())){
                at.setIsWord(true);
            }else{
                at.setIsWord(false);
            }
        }
        return list;
    }

    @Override
    public int checkTrain(ATrainInfor aTrainInfor) {
        Assert.notNull(aTrainInfor.getId(),"当前数据id不能为空！");
        Assert.notNull(aTrainInfor.getTrainWord(),"密码不能为空！");
        ATrainInfor infor = aTrainMapper.checkTrain(aTrainInfor);
        return infor != null ? 1 : 0;
    }
}
